﻿<?php

	/************************************************************************
							   Including POP
	************************************************************************/

	include('../../POP.php');
	
	/************************************************************************
							  Including classes
	************************************************************************/
	
	include('Person.php');
	include('Employee.php');
	include('Phone.php');
	include('Email.php');
	
	/************************************************************************
						  Configuring for verbose mode
	************************************************************************/
	POPEnvironment::$debug = true;

	/************************************************************************
							 Database connection
	************************************************************************/

	$GLOBALS['DB_TYPE']      = 'pgsql';
	$GLOBALS['DB_SERVER']    = '127.0.0.1';
	$GLOBALS['DB_NAME']      = 'pop_test';
	$GLOBALS['DB_USER']      = 'postgres';
	$GLOBALS['DB_PASSWD']    = 'postgres';

	//you must create a connection named 'pop_db' to be the default connection
	try
	{
		POPDB::addConnection('pop_db', $GLOBALS['DB_TYPE'], $GLOBALS['DB_SERVER'], $GLOBALS['DB_NAME'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
	}
	catch (PDOException $e)
	{
        echo 'There was an error trying to connect to the database: ' . $e->getMessage();
    }

	try
	{
		/**
		 * We need first to create an instance of each class 
		 */
		$per  = new Person();
		$emp  = new Employee();
		$pho  = new Phone();
		$mail = new Email();

		/**
		 * To create the database, we must first drop the tables
		 * or we'll have errors...
		 * Droping must be done on the reverse order of creation
		 * or you will get FK errors
		 */
		$mail->dropTable();
		$pho->dropTable();
		$emp->dropTable();
		$per->dropTable();
		
		/**
		 * Creation also has it rules
		 * 1st, objects that inherits from Persist and don't have parent classes
		 * 2nd, objects that inherits from the other objects, and also don't have parent classes
		 * 3rd, objects that inherits from Persist and have parent classes that have already been created
		 * 4th, objects that inherits from the other objects and have parent classes that have already been created
		 */
		$per->createTable();
		$emp->createTable();
		$pho->createTable();
		$mail->createTable();
		
		/**
		 * Now, let's test those objects!
		 */		
		$pho->number = '55 61 20201234';
		$mail->address = 'employee@popphp.com';
		
		$emp->name = "Employee's name";
		$emp->position = "PHP Developer";
		$emp->addAssociated('phones', $pho);
		$emp->addAssociated('emails', $mail);
		
		/**
		 * Saving everything with one single command...
		 */
		$emp->saveAll();

	}
	catch (Exception $e)
	{
		echo $e->getMessage();
		return false;
	}

?>